package com.engineering.elink.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.engineering.elink.common.Constant;
import com.engineering.elink.common.HessianResultEnum;
import com.engineering.elink.common.HessionInfluenceEnum;
import com.engineering.elink.common.MenberEnum;
import com.engineering.elink.common.MenberRoleEnum;
import com.engineering.elink.dao.CoinMapper;
import com.engineering.elink.dao.CoinRuleMapper;
import com.engineering.elink.dao.InfluenceMapper;
import com.engineering.elink.dao.PersonMapper;
import com.engineering.elink.dao.UserMapper;
import com.engineering.elink.domain.Coin;
import com.engineering.elink.domain.CoinRule;
import com.engineering.elink.domain.Influence;
import com.engineering.elink.domain.Person;
import com.engineering.elink.service.CoinBalanceService;
import com.engineering.elink.vo.HessianMessage;

@Service
public class CoinBalanceServiceImpl  implements  CoinBalanceService{

	@Autowired
	private CoinRuleMapper coinRuleMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private PersonMapper personMapper;
	@Autowired
	private CoinMapper coinMapper;
	@Autowired
	private InfluenceMapper influenceMapper;

	@Override
	@Transactional
	public HessianMessage modifyCoin(String fkId, int typeId, int coinValue, String userId, String byUserId) {
		if (fkId == null || userId == null || userId.equals(byUserId)) {
			return HessianMessage.getMessage(HessianResultEnum.ParamError);
		}
		// 转成正数
		coinValue = Math.abs(coinValue);
		// 匹配积分规则
		CoinRule coinRule = coinRuleMapper.selectByHessianTypeId(typeId);
		if (coinRule == null || coinRule.getIsDeduct() == -1) {
			return HessianMessage.getMessage(HessianResultEnum.TypeIdNotExist);
		}
		//用户积分上限(根据用户级别的不同，获取的积分不能超过总积分，)
		Person person = personMapper.selectByUserId(userId);
		//根据用户id，type类型，以及当天获取的分类的次数
		int TypeTotal= coinMapper.selectCount(userId,typeId);
		boolean  addCoin= true;
		if(typeId ==26 || typeId ==27){ //上传资料,论文
			if(person.getLevelId()== MenberEnum.REGULAR_MEMBERS.getIndex()){//普通会员
				if(TypeTotal ==MenberRoleEnum.REGULAR_MEMBERS_UPLOAD.getIndex()){//超过分类的次数
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-见习生
				if(TypeTotal ==MenberRoleEnum.REGISTER_TRAINEE_MEMBER_UPLOAD.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId()== MenberEnum.REGISTER_ASSISTANT_MEMBER.getIndex()){//实名注册会员-助理工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_ASSISTANT_MEMBER_UPLOAD.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_ENGINERR_MEMBER_UPLOAD.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId()== MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-高级工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_SENIOR_ENGINERR_MEMBER_UPLOAD.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-教授级高级工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_PROFESSOR_MEMBER_UPLOAD.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-工程大师
				if(TypeTotal ==MenberRoleEnum.REGISTER_MASTER_MEMBER_UPLOAD.getIndex()){
					addCoin = false;
				}
			}
		}else if(typeId == 32 || typeId == 33){//评论
			if(person.getLevelId()== MenberEnum.REGULAR_MEMBERS.getIndex()){//普通会员
				if(TypeTotal ==MenberRoleEnum.REGULAR_MEMBERS_COMMENT.getIndex()){//超过分类的次数
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-见习生
				if(TypeTotal ==MenberRoleEnum.REGISTER_TRAINEE_MEMBER_COMMENT.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId()== MenberEnum.REGISTER_ASSISTANT_MEMBER.getIndex()){//实名注册会员-助理工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_ASSISTANT_MEMBER_COMMENT.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_ENGINERR_MEMBER_COMMENT.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-高级工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_SENIOR_ENGINERR_MEMBER_COMMENT.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-教授级高级工程师
				if(TypeTotal ==MenberRoleEnum.REGISTER_PROFESSOR_MEMBER_COMMENT.getIndex()){
					addCoin = false;
				}
			}else if(person.getLevelId() == MenberEnum.REGISTER_TRAINEE_MEMBER.getIndex()){//实名注册会员-工程大师
				if(TypeTotal ==MenberRoleEnum.REGISTER_MASTER_MEMBER_COMMENT.getIndex()){
					addCoin = false;
				}
			}			
		}
		
		int currentCoinValue = 0;
		int userCoinValue = 0;
		Coin coin = new Coin();
		if (coinRule.getCoinValue() == 0) {
			userCoinValue = coinValue;
		} else {
			userCoinValue = coinRule.getCoinValue();
		}
		userCoinValue = userCoinValue * coinRule.getCoinType();
		coin.setFkId(fkId);
		coin.setCreateTime(new Date());
		coin.setUserId(userId);
		coin.setTypeId(typeId);
		coin.setCoinValue(userCoinValue);

		// 更改user积分
		Map<String, Object> userMap = userMapper.selectByUserId(userId);
		if (userMap == null || !userMap.containsKey("coinValue")) {
			return HessianMessage.getMessage(HessianResultEnum.UserIdNotExist);
		}
		currentCoinValue = (int) userMap.get("coinValue");
		currentCoinValue = currentCoinValue + userCoinValue;
		if (currentCoinValue < 0) {
			return HessianMessage.getMessage(HessianResultEnum.CoinNotEnough);
		}
		if(addCoin){//有条件增加积分
			coinMapper.insertSelective(coin);
			userMapper.updateCoinValueByUserId(userId, currentCoinValue);
		}
		
		// 清空数据
		currentCoinValue = 0;
		// 如果存在双向积分操作 更新另一用户积分
		if (coinRule.getIsDeduct() > 0) {
			CoinRule byUserCoinRule = coinRuleMapper.selectByHessianTypeId(typeId);
			if (byUserCoinRule == null) {
				return HessianMessage.getMessage(HessianResultEnum.TypeIdError);
			}
			Map<String, Object> byUserMap = userMapper.selectByUserId(byUserId);
			if (byUserMap == null || !byUserMap.containsKey("coinValue")) {
				return HessianMessage.getMessage(HessianResultEnum.UserIdNotExist);
			}
			currentCoinValue = (int) byUserMap.get("coinValue");
			userCoinValue = userCoinValue * -1;
			currentCoinValue = currentCoinValue + userCoinValue;
			if (currentCoinValue < 0) {
				return HessianMessage.getMessage(HessianResultEnum.CoinNotEnough);
			}
			coin.setCoinValue(userCoinValue);
			coin.setUserId(byUserId);
			coin.setTypeId((int) byUserCoinRule.getHessianTypeId());
			coinMapper.insertSelective(coin);
			userMapper.updateCoinValueByUserId(byUserId, currentCoinValue);
		}
		return HessianMessage.getMessage(HessianResultEnum.Success);
	}

	@Override
	public Map<String, Object> getCoinRule(int hessianTypeId) {
		return coinRuleMapper.selectByHessianTypeIdWithMap(hessianTypeId);
	}

	@Override
	public HessianMessage modifyInfluenceValue(String fkId, int typeId, String userId) {
		//判断用户今天获取的影响总值
		Person  person  = personMapper.selectByUserId(userId);
		Influence in = influenceMapper.selectByType(typeId,userId);
		 if((person.getTodayInfluence() < Integer.parseInt(Constant.todayInfluence)) && in == null){//如果影响值未达到上限并且同一个职能增加一次
			 
			  int addInfluence = 0;
			  if(typeId == HessionInfluenceEnum.LOGIN.getIndex()){//每日登陆
				  addInfluence =  Integer.parseInt(Constant.login);
			  }else if(typeId == HessionInfluenceEnum.UPLOADFILE.getIndex() || typeId == HessionInfluenceEnum.UPLOADTHESIS.getIndex()){//上传附件
				  addInfluence =  Integer.parseInt(Constant.upload); 
			  }else if(typeId == HessionInfluenceEnum.ADDCOMMENT.getIndex()){//评论
				  addInfluence =  Integer.parseInt(Constant.addComment);
			  }else if(typeId == HessionInfluenceEnum.SHARE.getIndex()){//分享
				  addInfluence =  Integer.parseInt(Constant.share);
			  }else if(typeId == HessionInfluenceEnum.ADDREPLY.getIndex()){//回复问题
				  addInfluence =  Integer.parseInt(Constant.addReply);
			  }else if(typeId == HessionInfluenceEnum.BESTREPLY.getIndex()){//最佳答案
				  addInfluence =  Integer.parseInt(Constant.bestReply);
			  }
			  person.setTodayInfluence(person.getTodayInfluence()+addInfluence);
			  personMapper.updateByPrimaryKeySelective(person);
			  //插入记录
			  Influence influence = new Influence();
			  influence.setFkId(fkId);//可能不存在
			  influence.setType(typeId);
			  influence.setUserId(userId);
			  influence.setTime(new Date());
			  influence.setValue(addInfluence);
			  influenceMapper.insertSelective(influence);
		 }
		return HessianMessage.getMessage(HessianResultEnum.Success);
	}
}
